In this video, I'm going to demonstrate the touched event and connect it to a function to make a part
disappear.
It's the basic template of a pickup.
So there's my part.
I'm going to touch it.
Boom.
It's gone pretty cool.
And then later we can add like cool stuff, like superpowers and things like that.
But first we've got to get the basics.
Let's turn this off and then get a fresh world and get started.
I have an empty world here.
Nothing in it.
Let's go to part.
Hit the little arrow and then add a sphere.
That's a good thing for a pickup right here.
Let's go to that part and name it.
Pickup.
Cool beans.
Let's make a brick color red and then we'll go to material.
We want it to glow.
So I'm gonna say.
And for neon, this is just a static, so it's not going to change anything.
And let's go to size.
Maybe we'll make a one by one by one instead of four by four by four.
A little more reasonable for a pickup to that move.
Move it up a little bit.
That's pretty good.
Anchored.
Let's anchor it in place so it doesn't fall down.
But I also want to be able to run through it so above anchored can collide.
I'm going to uncheck the touched event will still fire, right?
So you don't have to worry about that but if you forget to anchor it and can collide is off.
It's going to fall through your ground, it's going to disappear.
So just be careful with that.
All right.
So on our pickup, let's hit the plus sign script.
And let's call this simple pickup.
Cool.
Let's go here.
Get rid of that print statement in simple pickup and we'll get a variable for our pickup.
I'll call a pickup with a lowercase P script.
Parent.
There we go.
That puts a reference of our part into our variable, say, local function on touch.
Now you can name this whatever you want, but it should make sense.
But it's going to be catching the touched event.
So on.
Touched as good.
Other part is going to be the variable that's passed into on touch when the pickup is touched.
So you have two parts going on here, right?
You got the pickup and then you've got the other part to touch the pickup.
That's a little bit tricky sometimes.
All right.
Now down here below the on touch function, let's get the pickup dot touch event.
See that little lightning bolt?
That means it's an event.
We're going to do a colon connect to on touch.
Why use the dot?
Why use the colon?
What colons are for methods?
Dots are for functions.
They're pretty similar in Lua.
You're just going to have to kind of get used to it and you'll start figuring it out.
But it's going to take practice like a good definition is not going to help you.
You're just going to have to know.
All right.
So in my what's going to happen is a pickup is going to fire the touch event when it's touched, it's
going to connect to on touch function.
Roberts is going to know, hey, this is this is a touched event.
So we need the other part that's going to come, the parameter, the variable that gets passed in.
And then we can do a print to see what touched our part.
Oh, just did print twice.
Other part is.
Other part name.
And now that's going to print out what touched our pickup.
Let's go to view output play.
There's our part.
Run up.
Look at that.
We got a lot of things that just got printed out.
Right hand, right lower arm handle.
I'll explain what that is later.
Right upper arm.
Let's go to our workspace.
Right.
See workspace.
There's our character in there.
Let's open that up.
Look at that.
That's a lot of those parts.
Right, right hand.
Right hand.
Pretty cool.
We're going to get to know this pretty well, right?
I'm going to spend a lot of time on the character itself.
So thing is, part didn't disappear and we touched it many, many, many times.
We only want to touch it once, especially if it's giving superpowers or health and things like that.
So let's go to our on touch when somebody touches it or something touches it because we don't have a
filter right now, we are going to make it disappear.
Say, pick up colon, there's our crazy colon again.
Destroy.
We're going to destroy our pickup when someone touches it.
Let's try it.
Or something right now.
It could be anything.
Anything could touch.
You could kick a ball onto that.
It would still be destroyed by that left hand touched it.
It's gone.
And the event only fired once.
That's pretty good.
All right.
One more thing I want to show you in this video.
Let's take a look at our script.
Get rid of that.
You can do this two ways.
I'm going to duplicate this part.
Let's go to base plate.
Get the part.
Collisions are off, such as duplicate in place.
Control D drag this over here.
Let's make this pick up two so we don't get it confused.
Open that up.
Simple pickup two for the script, and then maybe I'll just make this a different color.
Like pink.
All right.
In our simple pickup to right, simple pickup two, I'm going to show you how to do an anonymous function.
So I'm breaking the function out.
I normally do that.
I connect with the function name.
But there's another way to do this.
All right, so this on touch, you could use an anonymous function with no name.
Just pass it in as an argument to the connect.
We'll get this the meat of the on touch function control see pasted here.
And now you don't need that external function with the name.
The downside is you can only use this here when you have a named function.
There's capability of calling it from all different types of places.
Also, if you're in a place that does unit tests, you might want to break out your function so you
could test the functions individually.
All right.
Let's try it, though.
Let's make this a little bit different, though.
Let's say pink, just so that we see that it was different.
That's pink.
Let's do pink.
Pink Koo play.
Here we go.
View output.
Let's get our pink one.
That worked fine.
Right.
Right hand.
And then this one.
Boom.
Work fine.
Upper torso.
So there's two ways of doing it.
Like I said, I will do the named functions.
I'm going to be more formal than less formal.
So I'm going to use this style more.
So I will see you in the next video.